package com.midian.common.utils;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

public class GeoAPI {
	/**百度地图AK*/
	public final static String AK ="8f740012d2133b7922d7cf107c4e4940";

	public static Map getLocationByLatLgn(String lat,String lgn){
		Map<String,String> locInfo= new HashMap<String,String>();
		String url="http://api.map.baidu.com/geocoder/v2/?ak="+AK+"&callback=renderReverse&location="+lat+","+lgn+"&output=json&pois=0";
		JSONObject retmsgJson=HttpClientUtils.getHttpResJson(url, "GET");
		if(!retmsgJson.getString("status").equals("0")){
			return null;
		}
		JSONObject address=JSONObject.fromObject( retmsgJson.get("result"));
		JSONObject addressComponent=JSONObject.fromObject( address.get("addressComponent"));
		locInfo.put("address",address.getString("formatted_address"));	
		locInfo.put("city", addressComponent.getString("city").substring(0, addressComponent.getString("city").length()-1));//市
		locInfo.put("district", addressComponent.getString("district"));//区
		locInfo.put("province", addressComponent.getString("province").substring(0, addressComponent.getString("province").length()-1));//省
		
		return locInfo;
	}
	public static void main(String[] args) {
		GeoAPI geoapi= new GeoAPI();
		Map<String,String> info=geoapi.getLocationByLatLgn("39.983784", "116.328287");
		System.out.println(info.get("city").substring(0, info.get("city").length()-1));
		
	}
}
